package com.example.date_save_test.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.date_save_test.user.User;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;


/**
 * @author: snjgm
 * data: 2022/7/18
 * description:
 */
public class UserDbHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "myDatabase";
    private static final int DB_VERSION = 2;
    public static final String TABLE_NAME = "user_info";
    private static UserDbHelper mHelper = null;
    private SQLiteDatabase mDB;

    //创建数据库，执行建表语句
    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_sql1 = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "name VARCHAR NOT NULL," +
                "age INTEGER NOT NULL," +
                "married INTEGER NOT NULL"+");";
        //执行完整的SQL语句
        db.execSQL(create_sql1);

    }
    //
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        String sql = "ALTER TABLE " +TABLE_NAME + " ADD COLUMN phone VARCHAR;";
        sqLiteDatabase.execSQL(sql);
    }


    private UserDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    //利用单例模式获取数据库帮助的唯一实例
    public static UserDbHelper getInstance(Context context){
        if (mHelper == null){
            mHelper = new UserDbHelper(context);
        }
        return mHelper;

    }

    //打开数据库的读连接
    public SQLiteDatabase openReadConnect(){
        if(mDB == null || !mDB.isOpen()){
            mDB = mHelper.getReadableDatabase();
        }
        return mDB;
    }

    //打开数据库的写连接
    public SQLiteDatabase openWriteConnect(){
        if(mDB == null || !mDB.isOpen()){
            mDB = mHelper.getWritableDatabase();
        }
        return mDB;
    }

//    关闭数据库连接
    public void closeLink(){
        if(mDB!=null && mDB.isOpen()){
            mDB.close();
            mDB = null;
        }
    }


    
    /**
      *
      * @description 储存数据
      * @param 
      * @return 
      * author snjgm
      * time 2022/7/19 下午4:02
      */
    public long insert(User user){
        ContentValues values = new ContentValues();
        values.put("name",user.name);
        values.put("age",user.age);
        values.put("married",user.married);
        return mDB.insert(TABLE_NAME,null,values);
    }

    /**
      *
      * @description 删除数据 
      * @param 
      * @return 
      * author snjgm
      * time 2022/7/21 下午1:28
      */
    public long deleteByName(String name){
        return mDB.delete(TABLE_NAME,"name = ?",new String[]{name});
    }
    /**
      *
      * @description 更新数据
      * @param 
      * @return 
      * author snjgm
      * time 2022/7/21 下午1:28
      */
    public Integer updateInfo(User user){
        ContentValues values = new ContentValues();
        values.put("name",user.name);
        values.put("age",user.age);
        values.put("married",user.married);
        return mDB.update(TABLE_NAME, values, "name=?", new String[]{user.name});
    }

    public List<User> queryAll(){
        List<User> queryList = new ArrayList<>();

        Cursor cursor = mDB.query(TABLE_NAME, null, null, null, null, null, null);
        while(cursor.moveToNext()){
            User user = new User();
            user._id = cursor.getInt(0);
            user.name = cursor.getString(1);
            user.age = cursor.getInt(2);
            user.married = (cursor.getInt(3)) == 0 ? false : true;
            queryList.add(user);
        }
        return queryList;
    }

}
